package com.niiwoo.civet.mobile.lanmao.handler;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.niiwoo.civet.account.dto.request.OpenCompanyAccountCallbackReqDTO;
import com.niiwoo.civet.account.service.AccountRegisterDubboService;
import com.niiwoo.tripod.lanmao.enums.AuditStatusEnum;
import com.niiwoo.tripod.lanmao.enums.BankCodeEnum;
import com.niiwoo.tripod.lanmao.enums.LanMaoGatewayServiceNameEnum;
import com.niiwoo.tripod.lanmao.enums.UserRoleEnum;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

/**
 * 企业注册绑卡回调
 * @author TomXue
 * @date 2018-04-26
 */
@Component
@Slf4j
public class EnterpriseRegisterNotifyHandler implements LanMaoNotifyHandler {
    @Reference(version = "1.0.0")
    AccountRegisterDubboService accountRegisterDubboService;

    @Override
    public boolean supports(String serviceName) {
            return LanMaoGatewayServiceNameEnum.ENTERPRISE_REGISTER.name().equals(serviceName);
    }

    @Override
    public HandleResult handle(JSONObject json) {
        log.info("企业绑卡注册回调,请求参数={}", json.toJSONString());
        EnterpriseRegisterGatewayResponse response = json.toJavaObject(EnterpriseRegisterGatewayResponse.class);
        HandleResult result = new HandleResult();
        try {
            log.info("企业绑卡注册回调,请求流水号={},请求参数={}", response.getRequestNo(),JSONObject.toJSONString(response));
            OpenCompanyAccountCallbackReqDTO openCompanyAccountCallbackReqDTO=new OpenCompanyAccountCallbackReqDTO();
            openCompanyAccountCallbackReqDTO.setRequestNo(response.getRequestNo());
            openCompanyAccountCallbackReqDTO.setCode(response.getCode());
            openCompanyAccountCallbackReqDTO.setStatus(response.getStatus());
            openCompanyAccountCallbackReqDTO.setErrorCode(response.getErrorCode());
            openCompanyAccountCallbackReqDTO.setErrorMessage(response.getErrorMessage());
            openCompanyAccountCallbackReqDTO.setPlatformUserNo(response.getPlatformUserNo());
            openCompanyAccountCallbackReqDTO.setAuditStatus(response.getAuditStatus().getValue());
            openCompanyAccountCallbackReqDTO.setUserRole(response.getUserRole().getValue());
            openCompanyAccountCallbackReqDTO.setBankcardNo(response.getBankcardNo());
            openCompanyAccountCallbackReqDTO.setBankcode(response.getBankcode().getCode());

            accountRegisterDubboService.openCompanyAccountCallback(openCompanyAccountCallbackReqDTO);
            result.setNeedAck(true);
            result.setRequestNo(response.getRequestNo());
        } catch (Exception e) {
            log.error("企业绑卡注册回调,请求流水号={},发生系统异常,原因={}",response.getRequestNo(),e);
        }
        log.info("企业绑卡注册回调,请求流水号={},响应结果={}", response.getRequestNo(),JSONObject.toJSONString(result));

        return result;
    }

    @Setter
    @Getter
    private static class EnterpriseRegisterGatewayResponse {

        private static final String CALL_SUCCESS_CODE = "0";
        private static final String BUSINESS_SUCCESS_CODE = "SUCCESS";

        // 调用状态(0 为调用成功、1 为失败,返回 1 时请看【调用失败错误码】及错误码)
        private String code;

        // 业务处理状态(处理失败 INIT;处理成功 SUCCESS),平台可根据非 SUCCESS 状态做相应处理,处理失败时可参考错误码
        private String status;

        // 错误码
        private String errorCode;

        // 错误码
        private String errorMessage;

        //平台用户编号
        private String platformUserNo;

        // 用户角色
        private UserRoleEnum userRole;

        //银行卡号
        private String bankcardNo;

        // 银行编码
        private BankCodeEnum bankcode;

        // 请求流水号
        private String requestNo;

        // 审核状态
        private AuditStatusEnum auditStatus;

        public boolean isCallSuccess() {
            return StringUtils.isNotBlank(code) && CALL_SUCCESS_CODE.equals(code);
        }

        public boolean isBusinessSuccess() {
            return StringUtils.isNotBlank(status) && BUSINESS_SUCCESS_CODE.equals(status);
        }

    }

}
